package com.xhs.service.impl;

import com.xhs.entity.SysUserEntity;
import com.xhs.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.xhs.entity.table.SysUserEntityTableDef.SYS_USER_ENTITY;

/**
 * @desc:
 * @projectName: spring-boot-redis
 * @author: xhs
 * @date: 2024-06-24 024 14:32
 * @version: JDK 1.8
 */
@Slf4j
@Service
public class CacheServiceImpl {
    @Resource
    private SysUserService userService;

    /**
     * 根据手机号查询用户信息，并缓存到redis
     *
     * @param phone 入参
     * @return SysUserEntity
     */
    @Cacheable(cacheNames = "user",key = "#phone", unless = "#result==null")
    public SysUserEntity getUserEntity(String phone) {
        SysUserEntity userEntity = userService.queryChain()
                .select(SYS_USER_ENTITY.DEFAULT_COLUMNS)
                .from(SYS_USER_ENTITY)
                .where(SYS_USER_ENTITY.IS_DELETED.eq(0))
                .and(SYS_USER_ENTITY.PHONE.like(phone))
                .one();
        log.info("<-----getUserEntity------>");
        return userEntity;
    }
}
